// pages/index/business/policy/index.js
import {articlePageList, articleList} from "../../../../utils/api/module/wenzhang";

Page({
    data: {
        policyList: '',
        title: '',
        page: 1,
        isLineData: 0,
        keywords: ''
    },
    onLoad: function (options) {
        console.log(options, 'options')
        this.setData({ title: options.title , id: options.id, type: options.type});
        wx.setNavigationBarTitle({ title: options.title });
        options.type == 2 ? this.getArticleLists() : this.getPolicyImage();
    },
    // 页面跳转
    go: function(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({ url: url });
    },
    getInputVal(e){
        this.data.page = 1;
        this.getArticleLists()
        // this.data.historyList.push(e.detail.value);
        // wx.setStorageSync('search-history', this.data.historyList);
    },
    input: function(e) {
        this.setData({
            keywords: e.detail.value
        })
    },
    /**
     * 获取文章列表有分页的
     */
    getArticleLists(){
        articlePageList({
            "columnid": this.data.id,
            "page": this.data.page,
            "limit": 10,
            keywords: this.data.keywords
        }).then( res => {
            if(res.data.data) {
                this.setData({
                    ['policyList[' + (this.data.page - 1) + ']']: res.data.data.rows,
                    isLineData: this.data.page !== 1 && res.data.data.rows.length > 10 ? 2 : (this.data.page == 1 && res.data.data.rows.length == 10 ? 1 : 0)
                })
                return false;
            };
            if(this.data.page == 1) {
                this.setData({
                    policyList: []
                });
                return false;
            };
            this.setData({
                ['policyList[' + (this.data.page - 1) + ']']: [],
                isLineData: 0
            })
            wx.showToast({title: res.data.message, icon: "none" })
        })
    },
    // 最新政策
    getPolicyImage: function() {
        articleList({
            columnid: 1,
            limit: 10,
            isrec: 1,
        }).then(res => {
            if(res.data) {
                this.setData({
                    ['policyList[' + (this.data.page - 1) + ']']: res.data.data,
                    isLineData: this.data.page !== 1 && res.data.data.length > 10 ? 2 : (this.data.page == 1 && res.data.data.length == 10 ? 1 : 0)
                })
            };
        })
    },
    onReachBottom: function() {
        if(this.data.policyList[this.data.page - 1].length == 10) {
            this.data.page += 1;
            this.setData({
                isLineData: 1
            });
            setTimeout(() => {
                if(this.data.type == 2) {
                    this.getArticleLists()
                }else{
                    this.getPolicyImage()
                }
            }, 1500)
            
        }else{
            this.setData({
                isLineData: 2
            });
        };
    }
})